package com.cong.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cong.project.model.entity.Collect;
import com.cong.project.model.entity.CollectNum;
import com.cong.project.model.entity.LikeNum;
import com.cong.project.model.entity.Likes;
import com.cong.project.model.enums.CollectStatusEnum;
import com.cong.project.model.enums.LikeStatusEnum;
import com.cong.project.model.request.TokenUserRequest;
import com.cong.project.service.CollectNumService;
import com.cong.project.service.CollectService;
import com.cong.project.mapper.CollectMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 86188
 * @description 针对表【collect(收藏表)】的数据库操作Service实现
 * @createDate 2022-11-01 14:22:44
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
        implements CollectService {
    @Resource
    private CollectNumService collectNumService;

    @Override
    public Integer collectUp(TokenUserRequest tokenUserRequest, Long postId, Integer type) {
        Long userId = tokenUserRequest.getId();

        Collect collectResult = this.getOne(new LambdaQueryWrapper<Collect>().eq(Collect::getUserId, userId).eq(Collect::getContentId, postId)
                .eq(Collect::getCollectType, type));

        synchronized (String.valueOf(userId).intern()) {
            //还没有点赞记录
            if (collectResult == null) {
                Collect collect = new Collect();
                collect.setContentId(postId);
                collect.setCollectType(type);
                collect.setUserId(userId);
                collect.setCollectStatus(CollectStatusEnum.IS_COLLECT.getCode());
                this.save(collect);
                boolean update = collectNumService.update(new UpdateWrapper<CollectNum>().eq("content_id", postId)
                        .eq("type", type).setSql("collect_num=collect_num+1"));
                return update ? 1 : 0;
            } else {
                //帖子已经点赞
                if (collectResult.getCollectStatus() == CollectStatusEnum.IS_COLLECT.getCode()) {
                    this.update(new UpdateWrapper<Collect>().eq("content_id", postId)
                            .eq("user_id", userId)
                            .eq("collect_type", type).setSql("collect_status=0"));
                    boolean update = collectNumService.update(new UpdateWrapper<CollectNum>().eq("content_id", postId)
                            .eq("type", type).setSql("collect_num=collect_num-1"));
                    return update ? -1 : 0;

                }
                //帖子未点赞
                if (collectResult.getCollectStatus() == CollectStatusEnum.NO_COLLECT.getCode()) {
                    this.update(new UpdateWrapper<Collect>().eq("content_id", postId)
                            .eq("user_id", userId)
                            .eq("collect_type", type).setSql("collect_status=1"));
                    boolean update = collectNumService.update(new UpdateWrapper<CollectNum>().eq("content_id", postId)
                            .eq("type", type).setSql("collect_num=collect_num+1"));
                    return update ? 1 : 0;


                }
            }
        }
        return 0;

    }
}




